package cc.vv.lkimagecomponent2.initplugin;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntRange;

import com.bumptech.glide.load.DecodeFormat;

import cc.vv.lkimagecomponent2.lib.Contants;
import cc.vv.lkimagecomponent2.lib.mode.DiskCacheMode;
import cc.vv.lkimagecomponent2.lib.mode.MemoryCategoryMode;


/**
 */

public class InitConfig {

    private Context context;

    /**
     * sdcard文件缓存 最大值
     */
    private int cacheMaxSize;

    /**
     * 磁盘缓存策略.默认All
     */
    private DiskCacheMode diskCacheStrategy;

    /**
     * 手机内存缓存等级
     */
    private MemoryCategoryMode memoryCategory;

    /**
     * 外存文件名
     */
    private String cacheFileName;

    /**
     * 加载失败资源id
     */
    @DrawableRes
    private int errorRes = 0;

    /**
     * 占位图资源id
     */
    @DrawableRes
    private int placeHolderRes = 0;

    /**
     * 加载失败资源Drawable
     */
    private Drawable errorDrawable;

    /**
     * 占位图资源Drawable
     */
    private Drawable placeHolderDrawable;

    /**
     * 图片显示质量 0-100 默认90
     */
    private int quality = 90;

    /**
     * gif播放次数.-1:无穷  0: 不播放  N:播放N次
     */
    private int gifRepeatCount = -1;

    /**
     * 初始化参数
     *
     * @param context
     * @param cacheSize           缓存最大的文件大小
     * @param memoryCategory      MemoryCategoryMode: 1:最大运存1.5倍（注重图片的app） 2：normal，1倍。 3：low，0.5倍。
     * @param diskCacheMode       图片缓存类型。all，result。。。
     * @param cacheFileName       默认存在外置sdcard:SDCard/Android/data/你的应用包名/images_cache/   如果传入此参数则为:
     * @param placeHolderRes
     * @param errorRes
     * @param placeHolderDrawable
     * @param errorDrawable
     */
    private InitConfig(Context context, int cacheSize, MemoryCategoryMode memoryCategory, DiskCacheMode diskCacheMode,
                       String cacheFileName, int placeHolderRes, int errorRes, Drawable placeHolderDrawable, Drawable errorDrawable
            , int quality, int gifRepeatCount) {
        this.context = context;
        this.memoryCategory = memoryCategory;
        this.cacheMaxSize = cacheSize;
        this.diskCacheStrategy = diskCacheMode;
        this.cacheFileName = cacheFileName;
        this.placeHolderRes = placeHolderRes;
        this.errorRes = errorRes;
        this.placeHolderDrawable = placeHolderDrawable;
        this.errorDrawable = errorDrawable;
        this.quality = quality;
        this.gifRepeatCount = gifRepeatCount;
    }


    public int getCacheMaxSize() {
        return cacheMaxSize;
    }

    public Drawable getPlaceHolderDrawable() {
        return placeHolderDrawable;
    }

    public int getQuality() {
        return quality;
    }

    public Drawable getErrorDrawable() {
        return errorDrawable;
    }

    public int getErrorRes() {
        return errorRes;
    }

    public int getPlaceHolderRes() {
        return placeHolderRes;
    }

    public String getCacheFileName() {
        return cacheFileName;
    }

    public DiskCacheMode getDiskCacheStrategy() {
        return diskCacheStrategy;
    }

    public MemoryCategoryMode getMemoryCategory() {
        return memoryCategory;
    }

    public int getGifRepeatCount() {
        return gifRepeatCount;
    }

    public Context getContext() {
        return context;
    }

    public static class InitBuilder {
        private Context context;

        /**
         * sdcard文件缓存 最大值
         */
        private int cacheMaxSize = Contants.MAX_CACHE;

        /**
         * 磁盘缓存策略.默认All
         */
        private DiskCacheMode diskCacheStrategy = DiskCacheMode.ALL;

        /**
         * 手机内存缓存等级
         */
        private MemoryCategoryMode memoryCategory = MemoryCategoryMode.NORMAL;
        /**
         * 外存文件名
         */
        private String cacheFileName = Contants.CACHE_FILE_NAME;

        /**
         * 加载失败资源id
         */
        @DrawableRes
        private int errorRes = 0;
        /**
         * 占位图资源id
         */
        @DrawableRes
        private int placeHolderRes = 0;

        /**
         * 加载失败资源Drawable
         */
        private Drawable errorDrawable;

        /**
         * 占位图资源Drawable
         */
        private Drawable placeHolderDrawable;

        /**
         * 图片显示质量
         */
        private int quality = 90;

        /**
         * gif播放次数.-1:无穷  0: 不播放  N:播放N次
         */
        private int gifRepeatCount = -1;


        public InitBuilder(Context context) {
            this.context = context;
        }

        /**
         * 设置最大缓存大小.默认450MB
         *
         * @param cacheMaxSize 单位MB
         * @return
         */
        public InitBuilder cacheMaxSize(int cacheMaxSize) {
            this.cacheMaxSize = cacheMaxSize;
            return this;
        }

        /**
         * 设置缓存策略.默认ALL
         *
         * @param diskCacheStrategy
         * @return
         * @see DiskCacheMode
         */
        public InitBuilder diskCacheStrategy(DiskCacheMode diskCacheStrategy) {
            this.diskCacheStrategy = diskCacheStrategy;
            return this;
        }

        /**
         * 设置app全局gif播放次数.  也可以在单独请求时去单独设置播放次数
         *
         * @param gifRepeatCount
         * @return
         */
        public InitBuilder gifRepeatCount(@IntRange(from = -1) int gifRepeatCount) {
            this.gifRepeatCount = gifRepeatCount;
            return this;
        }

        /**
         * 设置运存最大缓存等级
         *
         * @param memoryCategory
         * @return
         * @see MemoryCategoryMode
         */
        public InitBuilder memoryCategory(MemoryCategoryMode memoryCategory) {
            this.memoryCategory = memoryCategory;
            return this;
        }

        /**
         * 设置存放缓存目录名
         * <br>
         * 默认：SDCard/Android/data/你的应用包名/images/
         *
         * @param cacheFileName 参数不为空,则路径为 SDCard/Android/data/你的应用包名/cacheFileName/
         * @return
         */
        public InitBuilder cacheFileName(String cacheFileName) {
            this.cacheFileName = cacheFileName;
            return this;
        }

        /**
         * 设置项目中 默认的占位图.
         *
         * @param placeHolderRes
         * @return
         */
        public InitBuilder placeHolder(@DrawableRes int placeHolderRes) {
            this.placeHolderRes = placeHolderRes;
            return this;
        }

        /**
         * 设置项目中 默认的失败图.
         *
         * @param errorRes
         * @return
         */
        public InitBuilder error(@DrawableRes int errorRes) {
            this.errorRes = errorRes;
            return this;
        }

        /**
         * 设置项目中 默认的占位图.
         *
         * @param placeHolder
         * @return
         */
        public InitBuilder placeHolder(Drawable placeHolder) {
            this.placeHolderDrawable = placeHolder;
            return this;
        }

        /**
         * 设置项目中 默认的失败图.
         *
         * @param error
         * @return
         */
        public InitBuilder error(Drawable error) {
            this.errorDrawable = error;
            return this;
        }

        /**
         * 设置图片显示质量.默认90.
         *
         * @param quality
         * @return
         */
        public InitBuilder quality(@IntRange(from = 0, to = 100) int quality) {
            this.quality = quality;
            return this;
        }

        /**
         * 初始化设置参数结束后，必须调用此方法。
         */
        public void over() {
            InitConfig initConfig = new InitConfig(context, cacheMaxSize, memoryCategory, diskCacheStrategy, cacheFileName
                    , placeHolderRes, errorRes, placeHolderDrawable, errorDrawable, quality, gifRepeatCount);
            InitPlugin.getInstance().build(initConfig);
        }
    }
}
